package com.frxuexi.docment

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.frxuexi.docment.databinding.ActivityDocBinding
import com.frxuexi.docment.entity.DocEntity
import com.frxuexi.docment.intent.DocIntent
import kotlinx.coroutines.launch

@Route(path = "/doc/DocActivity")
class DocActivity : AppCompatActivity() {
    lateinit var vm:DocViewModel
    lateinit var db:ActivityDocBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        db=ActivityDocBinding.inflate(layoutInflater)
        setContentView(db.root)
        vm=ViewModelProvider(this).get(DocViewModel::class.java)

        lifecycleScope.launch {
            vm.docState.collect{
                when(it){
                    is DocState.onSuccess->{
                        Log.i("===",it.data.toString())
                        db.docCom.setContent {
                            var list by remember {
                                mutableStateOf(it.data)
                            }
                            DocPage(list,{ finish()}){
                               //删除
                                Log.i("===","去删除")
                               /* lifecycleScope.launch {
                                    vm.docChannel.send(DocIntent.deleteFile(it.fileId))
                                }*/
                                list-=it
                            }
                        }
                    }
                    is DocState.onDeleteSuccess->{
                        //vm.docChannel.send(DocIntent.getFiles)
                    }
                    is DocState.onError->{
                        Log.i("===",it.msg)
                    }
                    else->{}
                }
            }
        }

        lifecycleScope.launch {
            vm.docChannel.send(DocIntent.getFiles)
        }
    }
}